//
//  SDDefineAlertContentView.m
//  SDAlertView
//
//  Created by sander on 2017/10/23.
//

#import "SDDefineAlertContentView.h"

#import "SDCompositeService.h"


@interface SDDefineAlertContentView()

@property (nonatomic, weak) UILabel * theTitleLabel;

@property (nonatomic, weak) UILabel * theMessageLabel;

@property (nonatomic, strong) UIView * theContentView;

@end

@implementation SDDefineAlertContentView

- (instancetype)initWithTitle:(NSString * )title andMessage:(NSString * )message
{
    self = [super init];
    if (self) {
        _alert_title = title;
        _alert_message = message;
        
        [self sd_configAlertView];
    }
    return self;
}

- (instancetype)initWithTitle:(NSString * )title andContentView:(UIView * )contentView
{
    self = [super init];
    if (self) {
        _alert_title = title;
        
        [self addSubview:contentView];
        self.theContentView = contentView;
        
        
        [self sd_configCustomAlertView];
    }
    return self;
}

- (void)sd_configAlertView
{
    self.frame =CGRectMake(0, 0, screen_width - 20, 0);
    
    self.theTitleLabel.text = self.alert_title;
    
    CGSize size = [self.alert_title sizeWithAttributes:@{NSFontAttributeName:self.theTitleLabel.font}];
    
    self.theTitleLabel.frame =(CGRect){self.theTitleLabel.frame.origin,{self.theTitleLabel.frame.size.width,size.height}};

    
    self.theMessageLabel.text = self.alert_message;
    
    CGSize message_size = [self.alert_message boundingRectWithSize:CGSizeMake(self.bounds.size.width - 20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{NSFontAttributeName:self.theMessageLabel.font} context:nil].size;
    
    self.theMessageLabel.frame = (CGRect){{10,CGRectGetMaxY(self.theTitleLabel.frame) + 10},self.bounds.size.width - 20,message_size.height};
    
    
    
    self.frame = CGRectMake(0, 0, self.bounds.size.width, 10 + size.height + 10 + message_size.height + 10);
    
    
}

- (void)sd_configCustomAlertView
{
    self.frame = self.theContentView.bounds;
    
    CGFloat frame_height = 0;
    if (self.alert_title) {
        self.theTitleLabel.text = self.alert_title;
        
        CGSize size = [self.alert_title sizeWithAttributes:@{NSFontAttributeName:self.theTitleLabel.font}];
        
        self.theTitleLabel.frame =(CGRect){self.theTitleLabel.frame.origin,{self.theTitleLabel.frame.size.width,size.height}};
        
        frame_height = CGRectGetMaxY(self.theTitleLabel.frame);
        
    }
    
    self.theContentView.frame = (CGRect){{0,frame_height + 10},self.theContentView.frame.size};
    frame_height = frame_height + 10 + self.theContentView.frame.size.height + 10;
    
    self.frame = CGRectMake(0, 0, self.theContentView.frame.size.width, frame_height);
}

- (UILabel *)theTitleLabel
{
    if (!_theTitleLabel) {
        UILabel * theView = [[UILabel alloc] init];
        [self addSubview:theView];
        theView.font = [UIFont systemFontOfSize:20];
        theView.textColor = [UIColor blackColor];
        theView.frame = CGRectMake(0, 10, self.bounds.size.width, 0);
        theView.textAlignment = NSTextAlignmentCenter;
        
        _theTitleLabel = theView;
    }
    return _theTitleLabel;
}

- (UILabel *)theMessageLabel
{
    if (!_theMessageLabel) {
        UILabel * theView = [[UILabel alloc] init];
        [self addSubview:theView];
        theView.numberOfLines = 0;
        theView.textAlignment = NSTextAlignmentCenter;
        theView.font = [UIFont systemFontOfSize:17];
        theView.textColor = [UIColor blackColor];
        theView.frame = CGRectMake(0, 0, self.bounds.size.width, 0);
        _theMessageLabel = theView;
    }
    return _theMessageLabel;
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
    // Drawing code
}
*/

@end
